package DDSFileServer;

import jade.core.Agent;
import jade.core.behaviours.*;
import java.util.ArrayList;
import jade.lang.acl.*;
import java.util.Random;


/**
 * This class is an Agent. The Agent loads in the database, in this test setup the data base is only a list of files.
 * @author Rasmus Bo Sorensen
 * @author Bo Visfeldt
 */
public class FileServer extends Agent {

    private FileLoader fileLoader = new FileLoader();
    private ArrayList<File> fileList = new ArrayList();

    protected void setup(){
        // The two behaviors are added to the Agent.
        Object[] args = getArguments();
        SequentialBehaviour seq = new SequentialBehaviour();
        seq.addSubBehaviour(new Init(this, (String) args[0]));
        seq.addSubBehaviour(new Start(this));
        addBehaviour(seq);
    }

    class Init extends OneShotBehaviour{
        // The init "behaviour" loads the database = list of files.
        private String path;

        public Init(Agent a, String path){
            super(a);
            this.path = path;
        }

        public void action(){
            fileList = fileLoader.LoadFile(path);
        }
    }

    class Start extends CyclicBehaviour{
        // The Start "behaviour" waits for a Query from another Agent, and responds with the available bandwidth, if any.
        public Start(Agent a){
            super(a);
        }

        public void action(){
            ACLMessage msg = blockingReceive();
            ACLMessage reply = msg.createReply();
            if(msg.getPerformative() == ACLMessage.QUERY_REF){
                String aTitle = msg.getContent();
                reply.setPerformative(ACLMessage.INFORM);
                reply.setContent(String.valueOf(CheckFile(aTitle)));
            }
            else{
                reply.setPerformative(ACLMessage.NOT_UNDERSTOOD);
            }
            send(reply);
        }

        private int CheckFile(String aTitle){
            System.out.println("Searching for file: "+aTitle);
            for(File a:fileList){
                if(a.getName().equals(aTitle)){
                    System.out.println("File found. It needs "+a.getBandwidth()+" KB/s to download.");
                    int bandWidthAvailable = getMockBandWidth();
                    System.out.print("Checking bandwidth against requirements...");
                    if (a.getBandwidth()<bandWidthAvailable){
                       System.out.println("OK.");
                       return bandWidthAvailable;
                    }
                    else{
                        System.out.println("Not OK ... returning 0");
                        return 0;
                    }
                }
            }
            System.out.println("file not found, returning 0");
            return 0;
        }

        //makes a random bandwidth between 0-5000 unlesss doTest boolean is true
        private int getMockBandWidth(){
            Random rand = new Random();
            int temp = rand.nextInt(5000);
            System.out.println("Calculating MockBandWidth: "+temp);
            return temp;
        }
    }

}
